package com.weijun.keepalive_daemon.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.MotionEvent
import com.weijun.keepalive_daemon.isScreenOn
import com.weijun.keepalive_daemon.service.CoreService

/**
 * @author: weijun
 * date: 2023/7/5
 * description: 一像素的 Activity
 */
class OnePixelActivity : Activity() {

    companion object {
        private var onePixelActivity:OnePixelActivity ?= null
        fun finishIfExist(){
            onePixelActivity?.finish()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设定一像素的activity
        window.setGravity(Gravity.START or Gravity.TOP)
        window.attributes = window.attributes.apply {
            x = 0
            y = 0
            height = 1
            width = 1
        }
        onePixelActivity = this
    }

    override fun onResume() {
        super.onResume()
        if (isScreenOn()) {
            finish()
        }
    }

    override fun onDestroy() {
        startService(Intent(this, CoreService::class.java))
        super.onDestroy()
        onePixelActivity = null
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (!isFinishing) {
            finish()
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (!isFinishing) {
            finish()
        }
        return super.onTouchEvent(event)
    }



}